package org.example.Utils;

import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.example.persistence.model.reportSub;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class ReadExcelTest {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\12406\\Desktop\\标签汇总信息-营销系统待重构标签.xlsx";
        List<reportSub> reportSubs = new ArrayList<>();
        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(1);  // 选择Excel文件中的第一个工作表
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) { // 逐行遍历工作表
                Row row = rowIterator.next();
                if (row.getRowNum() == 0) continue; // 跳过表头
                reportSub reportSub = new reportSub();
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) { // 逐列读取单元格内容并赋值给 Person 对象
                    Cell cell = cellIterator.next();
                    int columnIndex = cell.getColumnIndex();
                    switch (columnIndex) {
                        case 0:
                            reportSub.setSubUserName(cell.getStringCellValue());
                            break;
                        case 1:
                            reportSub.setSubUserCode(cell.getStringCellValue());
                            break;
                        case 2:
                            reportSub.setId(cell.getStringCellValue());
                            break;
                        case 3:
                            reportSub.setReportNum(cell.getStringCellValue());
                            break;
                        default:
                            break;
                    }
                }
                reportSubs.add(reportSub); // 将 Person 对象添加到列表中
                if (row.getRowNum() >= 211) break;
            }
//            System.out.println(reportSubs); // 输出所有 Person 对象
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<String> collect = reportSubs.stream().map(reportSub::getId).distinct().collect(Collectors.toList());
        System.out.println(collect.size());

//        List<String> dataList = Lists.newArrayList();
//        String a;
//        for (reportSub reportSub : reportSubs) {
//            a = "`" + reportSub.getSubUserName() + "` " + reportSub.getId() + " COMMENT '" + reportSub.getSubUserCode() + "',";
//            dataList.add(a);
//        }
//
//        String filePathshufu = "C:\\Users\\12406\\Desktop\\test1.sql";
//
//        try (FileWriter writer = new FileWriter(filePathshufu)) {
//            for (String data : dataList) {
//                writer.write(data + System.lineSeparator()); // 写入每个字符串，并在其后面加上一个换行符
//            }
//            System.out.println(dataList.size());
//            System.out.println("数据已成功写入到文件 " + filePathshufu);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
}
